package com.ding.second.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.ding.second.common.UUIDUtil;
import com.ding.second.po.User;
import com.ding.second.service.ProductServer;
import com.ding.second.service.UserService;

@Controller
@RequestMapping("/userInfo")
public class UserInfoAction {

	@Autowired
	private UserService userServer;
	@Autowired
	private ProductServer productServer;

	@RequestMapping("/update")
	public String updateUserInfo(User user, @RequestParam("file") MultipartFile file, HttpServletRequest request)
			throws Exception {
		String realPath = request.getServletContext().getRealPath("/avatar");

		String fileName = file.getOriginalFilename();
		String currentName = UUIDUtil.getUUID() + fileName.substring(fileName.lastIndexOf("."), fileName.length());

		user.setUserState("101");
		user.setUserAvatar(currentName);
		boolean result = userServer.updateUserInfo(user);
		file.transferTo(new File(realPath + "/" + currentName));
		if (result) {
			return "redirect:/inquiry/all.do";
		}
		return "personInfo";
	}

	@RequestMapping("/searchProduct")
	public String getPersonProduct(HttpSession session, Model model) {
		int userId = (int) session.getAttribute("userId");
		Map<String, Object> productInfo = new HashMap<String, Object>();
		productInfo.put("userId", userId);
		List<Map<String, Object>> productList = productServer.getProjects(productInfo);

		for (int i = 0; i < productList.size(); i++) {
			String currentName = (String) productList.get(i).get("imageName");
			productList.get(i).put("imageName", "productImages/" + currentName);
		}
		
		User user = userServer.getUserByUserId(userId + "");
		user.setUserAvatar("avatar/" + user.getUserAvatar());

		model.addAttribute("productList", productList);
		model.addAttribute("user", user);
		return "personCenter";
	}

	@RequestMapping("/putOn/{productId}")
	public String putOn(@PathVariable String productId, HttpSession session) {
		int userId = (int) session.getAttribute("userId");
		Map<String, Object> productInfo = new HashMap<String, Object>();
		productInfo.put("productId", productId);
		productInfo.put("productState", "101");
		productInfo.put("userId", userId);
		productServer.updateProductState(productInfo);
		return "redirect:/userInfo/searchProduct.do";
	}

	@RequestMapping("/pullOff/{productId}")
	public String pullOff(@PathVariable String productId, HttpSession session) {
		int userId = (int) session.getAttribute("userId");
		Map<String, Object> productInfo = new HashMap<String, Object>();
		productInfo.put("productId", productId);
		productInfo.put("productState", "102");
		productInfo.put("userId", userId);
		productServer.updateProductState(productInfo);
		return "redirect:/userInfo/searchProduct.do";
	}

}
